package com.zsr.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.zsr.example.config.VodConfigProperties;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.config.exception.CommonJsonException;
import com.zsr.example.service.EduSectionService;
import com.zsr.example.service.VodService;
import com.zsr.example.util.AliyunVodSDKUtils;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.util.JwtUtil;
import com.zsr.example.util.constants.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.zsr.example.util.constants.SysLogConstants.DELETE;
import static com.zsr.example.util.constants.SysLogConstants.UPLOAD;

/**
 * 阿里云视频点播
 */
@RestController
@CrossOrigin
@RequestMapping("/vod")
public class VodController {

    @Autowired
    private VodService vodService;

    @Autowired
    private EduSectionService sectionService;

    /**
     * 上传视频到阿里云
     *
     * @param file
     * @return 视频id
     */
    @OptLog(optType = UPLOAD,optDesc = "上传视频")
    @PostMapping("uploadAliyunVideo")
    public JSONObject uploadAliyunVideo(MultipartFile file) {
        //net::ERR_CONNECTION_ABORTED 前端报错，可能是tomcat上传大小限制
        Map<String, Object> result = vodService.uploadAly(file);
        return CommonUtil.successJson(result);
    }

    /**
     * 删除单个视频
     *
     * @param videoId
     * @return
     */
    @DeleteMapping("{videoId}")
    public JSONObject removeVideo(
            @PathVariable String videoId) {

        vodService.removeVideo(videoId);
        return CommonUtil.success();
    }

    /**
     * 批量删除视频
     *
     * @param videoIdList
     * @return
     */
    @DeleteMapping("delete-batch")
    public JSONObject removeVideoList(
            @RequestParam("videoIdList") List<String> videoIdList) {

        vodService.removeVideoList(videoIdList);
        return CommonUtil.success();
    }
    @GetMapping("isHavePermissions/{id}")
    public JSONObject isHavePermissions(@PathVariable String id, HttpServletRequest request){
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        return CommonUtil.successJson( sectionService.isHavePermissions(id,memberId));
    }

    @GetMapping("getPlayAuth/{id}")
    public JSONObject getPlayAuth(@PathVariable String id, HttpServletRequest request) throws Exception {
        // 检查播放权限
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        boolean isHavePermissions = sectionService.isHavePermissions(id,memberId);

        if (!isHavePermissions) throw new CommonJsonException(ErrorEnum.E_80005);
        //获取阿里云存储相关常量
        String accessKeyId = VodConfigProperties.ACCESS_KEY_ID;
        String accessKeySecret = VodConfigProperties.ACCESS_KEY_SECRET;

        //初始化
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);

        //请求
        GetVideoPlayAuthRequest playAuthRequest = new GetVideoPlayAuthRequest();
        playAuthRequest.setVideoId(id);

        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(playAuthRequest);

        //得到播放凭证
        String playAuth = response.getPlayAuth();

        //返回结果
        return CommonUtil.successJson(playAuth);
    }
}